home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C23 / Catchref.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  604 b   |  38 lines

  1. //: C23:Catchref.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Why catch by reference?
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Base {
  11. public:
  12.   virtual void what() {
  13.     cout << "Base" << endl;
  14.   }
  15. };
  16.  
  17. class Derived : public Base {
  18. public:
  19.   void what() {
  20.     cout << "Derived" << endl;
  21.   }
  22. };
  23.  
  24. void f() { throw Derived(); }
  25.  
  26. int main() {
  27.   try {
  28.     f();
  29.   } catch(Base b) {
  30.     b.what();
  31.   }
  32.   try {
  33.     f();
  34.   } catch(Base& b) {
  35.     b.what();
  36.   }
  37. } ///:~
  38.